#include <stdio.h>
#include <stdlib.h>
#include "main.h"
#include "device.h"
#include "link.h"
#include "select.h"
#include "socket.h"
#include "unistd.h"
#include "player.h"
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#include <sys/select.h>
#include <signal.h>


//struct Node* head;

int g_sockfd;

extern int shmid;
extern shm *shmaddr;

//删除共享内存
void handler(int sig)
{
        printf("SIGINT处理\n");
        kill(shmaddr->child_pid,SIGKILL);
        kill(shmaddr->grand_pid,SIGKILL);
        
        shmdt(shmaddr);
        shmctl(shmid,IPC_RMID,NULL);
        close(g_sockfd);
        exit(0);
}

int main()
{
        int ret;
        
        ret=InitDriver();//打开设备文件
        if(ret==-1){
                printf("设备文件打开失败\n");
                exit(-1);
        }

        ret=InitSocket();//初始化网络
        if(ret==FAILURE)
        {
                printf("初始化网络失败\n");
        }
        //初始化链表
        ret=initLink();
        if(ret==FAILURE){
                printf("链表初始化失败\n");
                exit(-1);
        }
        
        //初始化共享内存
        ret = InitShm();
	if (FAILURE == ret)
	{
		printf("共享内存初始化失败\n");
		exit(1);
	}

        //获得音乐
        getMusic();

        //设置捕获ctrl c信号后的处理函数
        signal(SIGINT,handler);

        my_select();

        return 0;
}